package com.hboxs.asl.controller.asl;

import com.hboxs.asl.*;
import com.hboxs.asl.Order;
import com.hboxs.asl.entity.*;
import com.hboxs.asl.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller("rootController")
@RequestMapping("/weChat")
public class RootContorller extends BaseController {

    @Resource(name = "productServiceImpl")
    private ProductService productService;
    @Resource(name = "productCategoryServiceImpl")
    private ProductCategoryService productCategoryService;
    @Resource(name = "adServiceImpl")
    private AdService adService;
    @Resource(name = "adPositionServiceImpl")
    private AdPositionService adPositionService;
    @Resource(name = "cartServiceImpl")
    private CartService cartService;
    @Resource(name = "recommendCommodityServiceImpl")
    private RecommendCommodityService recommendCommodityService;
    @Resource(name = "memberServiceImpl")
    private MemberService memberService;

    private CartItemService cartItemService;


    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String root() {
        return "redirect:/index.htm";
    }

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(ModelMap modelMap) {
        try {
            Member member = memberService.getCurrent();
            if(member!=null){
                modelMap.addAttribute("count", cartService.getTotalNumberOfGoodsInCart(member.getCart()));
            }

            modelMap.addAttribute("categories", productCategoryService.findTopProductCategory());
            modelMap.addAttribute("reomms", recommendCommodityService.findCurRecomCmdt());
            modelMap.addAttribute("videos", adService.findListByAdPosition(Constant.WECHAT_AD_POSITION_BANNER));
            modelMap.addAttribute("ads", adService.findListByAdPosition(Constant.WECHAT_AD_POSITION_MIDDLE));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/yijing/index";
    }


    /**
     * 分类页面
     *
     * @return
     */
    @RequestMapping(value = "/class", method = RequestMethod.GET)
    public String cls(Pageable pageable, ModelMap modelMap) {
        List<ProductCategory> productCategories = productCategoryService.findList(null, null, null);
        pageable.getOrders().add(Order.desc("sales"));
        Page<Product> products = productService.findPage(pageable);
        modelMap.addAttribute("products", products);
        modelMap.addAttribute("productCategories", productCategories);
        return "/yijing/product-list";
    }


    /**
     * 商品详情 - web查看内容
     *
     * @return
     */
    @RequestMapping(value = "/product_detail", method = RequestMethod.GET)
    public String introduction(Long pId, ModelMap model, HttpSession session) {
        Product product = productService.find(pId);
        if (product == null) {
            return "yijing/error";
        }

        // 商品属性
        Map<String, String> attributeValue = new HashMap<String, String>();
        Set<Attribute> attributes = product.getProductCategory().getAttributes();
        for (Attribute attribute : attributes) {
            String value = product.getAttributeValue(attribute);
            value = value.replaceAll(",", " ");
            attributeValue.put(attribute.getName(), value);
        }

        model.addAttribute("attributeValue", attributeValue);
        model.addAttribute("product", product);
        model.addAttribute("introduction", product.getIntroduction());
        return "/yijing/product-detail";
    }

    /**
     * 规格页面
     *
     * @param pId 产品id
     */
    @RequestMapping(value = "/spec", method = RequestMethod.GET)
    public String spec(Long pId, ModelMap model) {
        model.addAttribute("product", productService.find(pId));
        return "yijing/spec";
    }

    /**
     * 广告详情，商品列表
     *
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/ad-list", method = RequestMethod.GET)
    public String adProduct(ModelMap modelMap , Long adId , Product.OrderType orderType,
                            Pageable pageable) {
        Ad ad = adService.find(adId);
        if(ad==null){
            return ERROR_VIEW;
        }

        ProductCategory productCategory = ad.getProductCategory();
        Page page = productService.findPageGroup(
                productCategory, null, null, null, null, null, null, true, null, null, null, null, orderType, pageable
        );

        modelMap.addAttribute("totalPages", page.getTotalPages());
        modelMap.addAttribute("page" , page);
        modelMap.addAttribute("orderType", orderType);
        modelMap.addAttribute("productCategory" , productCategory);
        modelMap.addAttribute("ad" , ad);
        return "/yijing/ad-product-list";
    }


}